package com.suse.forum.controller;


import com.suse.forum.common.AppResult;
import com.suse.forum.common.ResultCode;
import com.suse.forum.config.AppConfig;
import com.suse.forum.model.Article;
import com.suse.forum.model.ArticleReply;
import com.suse.forum.model.User;
import com.suse.forum.service.IArticleReplyService;
import com.suse.forum.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.function.Predicate;

@Api(tags = "回复接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private IArticleService articleService;

    @Resource
    private IArticleReplyService articleReplyService;

    @ApiOperation("回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("帖子ID") @RequestParam("articleId") @NonNull Long articleId,
                            @ApiParam("帖子内容") @RequestParam("content") @NonNull String content,
                            @ApiParam("帖子内容")  Long replyId) {
        // 获取用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        // 判断用户是否已禁言
        if (user.getState() == 1) {
            // 表示已禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        // 获取要回复的帖子对象
        Article article = articleService.selectById(articleId);
        // 是否存在,或已删除
        if (article == null || article.getDeleteState() == 1) {
            // 表示已删除或不存在
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 是否封贴
        if (article.getState() == 1) {
            // 表示已封贴
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }

        // 构造回复对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId); // 要回复的帖子ID
        articleReply.setPostUserId(user.getId()); // 回复的发送者
        articleReply.setContent(content); // 回复内容
        articleReply.setReplyId(replyId); // 回复内容
        // 写入回复
        articleReplyService.create(articleReply);
        // 返回结果
        return AppResult.success();
    }

    @ApiOperation("获取回复列表")
    @GetMapping("/getReplies")
    public AppResult<Collection<ArticleReply>> getRepliedByArticleId(@ApiParam("帖子ID") @RequestParam("articleId") @NonNull Long articleId) {
        // 校验帖子是否存在
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            //返回错误结果
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 调用sevice
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        Collection<ArticleReply> newArts = new ArrayList<>();


        Map<Long, ArticleReply> articleReplyMap = new HashMap<>();
        if(articleReplies.size() > 0){


            articleReplies.removeIf(new Predicate<ArticleReply>() {
                @Override
                public boolean test(ArticleReply articleReply) {
                    if(articleReply.getReplyId() == null){
                        articleReplyMap.put(articleReply.getId(), articleReply);
                        return true;
                    }
                    return false;
                }
            });
            for (ArticleReply articleReply : articleReplies) {
                if(articleReplyMap.containsKey(articleReply.getReplyId())){
                    ArticleReply art = articleReplyMap.get(articleReply.getReplyId());
                    art.getSubs().add(articleReply);
                }
            }
            newArts = articleReplyMap.values();
        }
        // 返回结果
        return AppResult.success(newArts);
    }
}
